package org.huangrui.javase;

import java.util.HashSet;

/**
 * @Author hr
 * @Create 2025-11-01 16:42
 **/
public class T04HashConflict {
    static class Book{
        int id = 1;
    }
    public static void main(String[] args) {
        // m1();
        // 请写出一个hashcode冲突案例
        // 当我们正常的new对象的时候，new多少次对象，会有可能出现hash冲突???
        HashSet<Integer> hashSet = new HashSet<>();
        for (int i = 0; i < 110000; i++) {
            int bookHashCode = new Book().hashCode();
            if (!hashSet.contains(bookHashCode)){
                hashSet.add(bookHashCode);
            }else {
                System.out.println("发生了hash冲突，在第："+i+"次，值是："+ bookHashCode);
            }
        }
        System.out.println(hashSet.size());
    }

    private static void m1() {
        // 请写出一个hashcode冲突案例
        System.out.println("AA".hashCode());
        System.out.println("BB".hashCode());
        System.out.println();

        System.out.println("Aa".hashCode());
        System.out.println("BB".hashCode());
        System.out.println();

        System.out.println("柳柴".hashCode());
        System.out.println("柴柕".hashCode());
    }
}
